package com.mate.cloud.lettuce;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.support.ConnectionPoolSupport;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;


public class LettucePool {
    public static void main(String[] args) throws Exception {

        // 1. 构建 Redis 连接信息（明确指定主机、端口、密码）
        RedisURI redisURI = RedisURI.builder()
                .withHost("192.168.110.36")    // 主机地址
                .withPort(6379)                // 端口
                .withPassword("seata@user123")  // 密码（如果没有密码，可省略此句）
                .withDatabase(0)               // 数据库索引（默认0，可选）
                .build();
        RedisClient client = RedisClient.create(redisURI);
        // 使用连接池
        GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
                .createGenericObjectPool(() -> client.connect(), new GenericObjectPoolConfig());

        try (StatefulRedisConnection<String, String> connection = pool.borrowObject()) {
            RedisCommands<String, String> commands = connection.sync();
            commands.set("qingshan", "2673");
            System.out.println(commands.get("qingshan"));
            commands.zadd("gupao", 100, "Java");
            commands.zadd("gupao", 80, "Python");
            System.out.println("---------------------"+commands.zcard("gupao"));
        }
        pool.close();
        client.shutdown();
    }
}
